<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 单项管理
 */
namespace app\api\model;

use app\api\model\Model;

class Web extends Model{

    private $category = 'pro_category';
    private $trade = 'pro_trade';
    private $tradeinfo = 'pro_tradeinfo';

    private $adtable = 'web_advert';
    private $adclasstable = 'web_advertclass';
    private $article = 'sys_article';
    private $articleclass = 'sys_articleclass';
    private $single = 'web_single';
    private $contractcate = 'sys_contractcate';
    private $contract = 'sys_contract';
    
    private $basics = 'web_basics';
    private $online  = 'user_online'; //在线表
    private $custer  = 'sys_custer'; //客服表
    private $sysbasics  = 'sys_basics'; //客服表

    private $menu = 'web_menu';

    private $links = 'website_links';
    private $linksclass = 'website_linksclass';

    private $counttable = 'web_count';

    /**
     * 基础信息
     * @param array $basics
     * @param array $where
     */
    public function getconfig($where='')
    {
        $basics = Model::name($this->basics)
            ->field('*')
            ->where($where)
            ->find();
        return $basics;
    }

    /**
     * 客服信息
     * @param array $custer
     * @param array $where
     */
    public function getcuster($where='',$lang='zhtw')
    {
        $custer = Model::name($this->custer)
            ->field('*')
            ->where($where)
            ->select()
            ->toArray();
        foreach ($custer as $key => $value) {
            if(!empty($value['titles'])){
                $custer[$key]['name']=isset(json_decode($value['titles'],true)[$lang])?json_decode($value['titles'],true)[$lang]:$value['name'];
            }
            if(!empty($value['kefu_urls'])){
                $custer[$key]['kefu_url']=isset(json_decode($value['kefu_urls'],true)[$lang])?json_decode($value['kefu_urls'],true)[$lang]:$value['kefu_url'];
            }
            unset($custer[$key]['titles'],$custer[$key]['kefu_urls']);
        }
        return $custer;
    }

    /**
     * 菜單列表
     * @param array $menuList
     * @param array $where
     */
    public function menuList($where='',$field='*')
    {
        $menuList = Model::name($this->menu)
            ->field($field)
            ->where($where)
            ->order('orderid asc, id asc')
            ->select()
            ->toArray();
        return $menuList;
    }

    /**
     * 获取基础语言列表
     * @param array $getBase 
     * @param array $where 
    */ 
    public function getLang(){ 
        $lang=[];
        $langs=[];
        $lang = Model::name($this->sysbasics)
            ->field('content,notes')
            ->where('keyname="lang"')
            ->find();
        if(!empty($lang['content'])){
            $lang['notes'] = json_decode($lang['notes'],true);
            $lang['content'] = json_decode($lang['content'],true);
            foreach ($lang['content'] as $key => $value) {
                $langs[$key]['value']=$value;
                $langs[$key]['name']=$lang['notes'][$key];
            }
            unset($lang['content'],$lang['notes']);
        }else{
            $langs = config('langs');
        }
        return $langs;
    }
    
    /**
     * 产品类别列表
     * @param array $catelist 
     * @param array $where 
     */ 
    public function catelist($where=null, $page=1, $page_size=10, $field='*'){
        //矿机列表
        $list['data']= Model::name($this->category)
            ->alias('a')
            ->field($field)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('a.orderid asc')
            ->where($where)
            ->select()
            ->toArray();
        //数据总数
        $list['total']=Model::name($this->category)
            ->alias('a')
            ->where($where)
            ->count('a.id');
        return $list;
    }

    /**
     * 产品列表
     * @param array $trade 
     * @param array $where 
     */ 
    public function trade($where=null, $page=1, $page_size=10, $field='*'){
        //矿机列表
        $list['data']= Model::name($this->trade)
            ->alias('a')
            ->field($field)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('a.status asc,a.orderid desc')
            ->where($where)
            ->order('a.orderid desc')
            ->select()
            ->toArray();
        //数据总数
        $list['total']=Model::name($this->trade)
            ->alias('a')
            ->where($where)
            ->count('a.id');
        return $list;
    }

    /**
     * 产品列表
     * @param array $trade 
     * @param array $where 
     */ 
    public function protrade($where=null, $page=1, $page_size=10, $field='*'){
        //矿机列表
        $list['data']= Model::name($this->tradeinfo)
            ->alias('a')
            ->field($field)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('a.status asc,a.orderid desc')
            ->where($where)
            ->order('a.orderid desc')
            ->select()
            ->toArray();
        //数据总数
        $list['total']=Model::name($this->tradeinfo)
            ->alias('a')
            ->where($where)
            ->count('a.id');
        return $list;
    }

    /**
     * 获取内容類別
     * @param array $getcateinfo 
     * @param array $where 
     */ 
    public function getcatelist($where='', $file='*')
    {
        $cateinfo = Model::name($this->articleclass)
            ->field($file)
            ->where($where)
            ->select()
            ->toArray();
        return $cateinfo;
    }

    /**
     * 获取内容類別
     * @param array $getcateinfo 
     * @param array $where 
     */ 
    public function getcateinfo($where='', $file='*')
    {
        $cateinfo = Model::name($this->articleclass)
            ->field($file)
            ->where($where)
            ->find();
        return $cateinfo;
    }

    /**
     * 内容列表
     * @param array $arlist
     * @param array $where
     */
    public function arlist($where='',$page=1,$page_size=10, $file='*')
    {
        $articlelist['data'] = Model::name($this->article)
            // ->field('open_url,introduction,titles,title,is_open,id,cid,url')
            ->field($file)
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('createtime desc')
            ->select()
            ->toArray();
         //数据总数
        $articlelist['total']=Model::name($this->article)
            ->where($where)
            ->count('id');
        return $articlelist;
    }

    /**
     * 获取文档類別
     * @param array $getconcate 
     * @param array $where 
     */ 
    public function getconcate($where='', $file='*')
    {
        $cateinfo = Model::name($this->contractcate)
            ->field($file)
            ->where($where)
            ->select()
            ->toArray();
        return $cateinfo;
    }

    /**
     * 文档列表
     * @param array $concatelist
     * @param array $where
     */
    public function concatelist($where='',$page=1,$page_size=10, $file='*')
    {
        $articlelist['data'] = Model::name($this->contract)
            // ->field('open_url,introduction,titles,title,is_open,id,cid,url')
            ->field($file)
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('createtime desc')
            ->select()
            ->toArray();
         //数据总数
        $articlelist['total']=Model::name($this->contract)
            ->where($where)
            ->count('id');
        return $articlelist;
    }

    /**
     * 首页内容列表
     * @param array $indexarticle
     * @param array $where
     */
    public function indexarticle($where='',$page=1,$page_size=10)
    {
        $articlelist = Model::name($this->article)
            ->field('*')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('createtime desc')
            ->select()
            ->toArray();
        foreach ($articlelist as $key => $value) {
            if($value['is_open']>0){
                $articlelist[$key]['url']=$value['open_url'];
            }
        }
        return $articlelist;
    }

}
